<?php


namespace app\mall\controller\Index;


use app\mall\controller\BaseController;
use app\mall\model\Order\OrderModel;
use app\mall\model\User\UserModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class Index extends BaseController
{
    /**
     * 首页统计
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $data = [];
        //代发货
        $statistics['order_num'] = OrderModel::getOrderCountWhere([['type','=',1],['status','=',1]]);
        //用户数量
        $statistics['user_num'] = UserModel::getUserCountWhere([['is_show','=',1]]);
        //会员数量
        $statistics['vip_num'] = UserModel::getUserCountWhere([['level_id','<>',0],['end_time','>',date("Y-m-d H:i:s",time())]]);
        //过期会员数量
        $statistics['past_vip_num'] = UserModel::getUserCountWhere([['level_id','<>',0],['is_level_type','=',0],['end_time','<',date("Y-m-d H:i:s",time())]]);
        //课程报名
        $statistics['course_num'] = OrderModel::getOrderCountWhere([['type','=',3],['status','=',1]]);
        $data['statistics'] = $statistics;
        return Util::returnJson(self::CODE_DATA,$data);
    }

    /**
     * 上传图片
     * @param Request $request
     * @return array
     */
    public function upload(Request $request)
    {
        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "goods/product/attr/img/";
            $url = ImageUtil::i()->upload($file,$path);
            return Util::returnJson(self::CODE_SUCCESS,$url);
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"请选择图片");
        }
    }

    /**
     * 文件上传
     * @param Request $request
     * @return array
     */
    public function uploadFileSimple(Request $request)
    {
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "app/version/";
            $url = ImageUtil::i()->upload($file, $path);
        }
        return Util::returnJson(self::CODE_SUCCESS,$url);
    }

}